package merisis.experiments.datastruct;

import static java.lang.String.format;
import static java.lang.System.err;

import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.Random;

import junit.framework.Assert;

import merisis.silo.CakedCursor;
import merisis.silo.CakedCursorUtil;
import merisis.silo.SequenceSpace;

import org.junit.Ignore;
import org.junit.Test;

public class DictionaryExperiment1 {

	@Test
	@Ignore
	public void testLPOTransformation() {

		CakedCursor ptr = null;

		// ptr = new Cursor();

		// System.out.println("NODE_BLOCK_NUM = " + CakedCursorUtil.MAX_LEVEL);
		//
		// long start = 0;
		// long end = 1000000;
		// for (long pos = start; pos <= end; pos++) {
		//
		// CakedCursor.transformToOffsetInScale(pos, ptr);
		// Assert.assertEquals(pos, CakedCursor.transform(ptr));
		// }
	}

	@Test
	@Ignore
	public void addElementOnHeadAndSearchElement() {

		SequenceSpace ds = new SequenceSpace();

		CakedCursor cur;
		cur = ds.openCursor(new byte[] { 0 });
		int m = 10240;
		int k = m;
		String val;
		for (int i = 0; i < m; i++) {
			val = String.format("s-%05d", k--);
			ds.add(cur, val);
		}

		val = String.format("s-%05d", k--);
		ds.add(cur, val);

		printAllEntities(ds);

		CakedCursor located = ds.openCursorOnBottom();
		boolean r;
		r = ds.search(ds.openCursorOnBottom(), ds.openCursorOnTop(), "s-00048",
				located);
		System.out.println(String.format("%s: %s", r, located));
	}

	@Test
	// @Ignore
	public void doAppendRandomly() {

		long[] data = genRandArray(1000, 200000, 10200);

		String fmt = "%010d";
		SequenceSpace ds = new SequenceSpace();
		for (int i = 0; i < data.length; i++) {

			String elem;
			elem = String.format(fmt, data[i]);
			// System.out.println(String.format("add[%05d]: %s", i, elem));
			ds.append(elem);
		}

		// printAllEntities(ds);

		Arrays.sort(data);
		CakedCursor cur;
		int i = 0;
		for (cur = ds.openCursorOnBottom(); cur.hasNext(); cur.nextEntity()) {

			final long num = data[i];
			String test = String.format(fmt, num);
			String entity = (String) cur.get();
			if (!test.equals(entity)) {
				String msg = format(
						"MISMATCHED!!!  expected (%010d:%010d), but (%15s, %s)",
						i, num, cur.toString(), entity);
				err.println(msg);
				Assert.fail(msg);
			}

			do {
				// remove the duplicated number
				i++;
			} while (i < data.length && num == data[i]);
		}
	}

	public static void printAllEntities(SequenceSpace ds) {

		CakedCursor cur;
		for (cur = ds.openCursorOnBottom(); cur.hasNext(); cur.nextEntity()) {

			String entity = (String) cur.get();
			System.out.println(format("%15s => %s", cur.toString(), entity));
		}
	}

	public static long[] genRandArray(long minval, long maxval, int len) {

		long[] a = new long[len];

		long scope = maxval - minval + 1;
		Random rand = new Random();

		int count = 0;
		while (count < len) {

			long elem = minval + rand.nextInt((int) scope);

			a[count] = elem;

			count++;
		}

		return a;
	}
}
